package com.zr.kufangkuqu.controller;

import com.zr.kufangkuqu.pojo.KuQuAdd;
import com.zr.kufangkuqu.pojo.KuQuSelectVo;
import com.zr.kufangkuqu.pojo.KuQuStatusVo;
import com.zr.kufangkuqu.service.KuQuService;
import com.zr.meiju.StatusEnum;
import com.zr.meiju.XiaLaVo;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by star on 2019/7/16.
 */
@RestController
public class KuQuController {
    @Autowired
    private KuQuService kuQuService;
    /**
     *
     * 库区属性----状态----下拉框
     *
     */
    @GetMapping("StatusEnum")
    public ResultVo<List<XiaLaVo>> StatusEnum(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< StatusEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setValue(i);
            xiaLaVo.setLabel(StatusEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }
    /**
     *
     * 库区属性----分页
     *
     */
    @PostMapping("pickingArea/queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody KuQuSelectVo kuQuSelectVo){

        return kuQuService.queryPage(kuQuSelectVo);
    }
    /**
     * 查看
     */
    @RequestMapping("queryKuQuById")
    public ResultVo queryKuQuById(@RequestParam("id")Integer id){

        return kuQuService.queryKuQuById(id);
    }
    /**
     *
     * 修改状态接口
     *
     */
    @PostMapping("updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid KuQuStatusVo kuQuStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return kuQuService.updateStatus(kuQuStatusVo);
    }

    /**
     *
     * 新增
     *
     */
    @PostMapping("addKuQu")
    public ResultVo addKuQu(@RequestBody @Valid KuQuAdd kuQuAdd, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return kuQuService.addKuQu(kuQuAdd);
    }








}
